/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.guobo.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.modules.guobo.dao.ExbExhDao;
import com.thinkgem.jeesite.modules.guobo.dao.ExhHallDao;
import com.thinkgem.jeesite.modules.guobo.dao.ExhibitionDao;
import com.thinkgem.jeesite.modules.guobo.entity.ExbExh;
import com.thinkgem.jeesite.modules.guobo.entity.ExhHall;
import com.thinkgem.jeesite.modules.guobo.entity.Exhibition;

/**
 * 展会Service
 * @author wudavid
 * @version 2017-05-10
 */
@Service
@Transactional(readOnly = true)
public class ExhibitionService extends CrudService<ExhibitionDao, Exhibition> {

	@Autowired
	private ExhHallDao exhHallDao;
	
	@Autowired
	private ExbExhDao exbExhDao;
	
	public Exhibition get(String id) {
		/*Exhibition exhibition = super.get(id);
		ExbExh exbExh = new ExbExh();
		exbExh.setExhibition(exhibition);
		List<ExbExh> exbexhs = exbExhDao.findExHallsByExbition(exbExh);
		if(exbexhs!=null && exbexhs.size()>0){
			String exhIds="";
			String exhNames = "";
			for (ExbExh exbh : exbexhs) {
				exhIds = exhIds+","+exbh.getExhHall().getId();
				exhNames = exhNames+","+exbh.getExhHall().getName();
			}
			exhibition.setExhIds(exhIds.substring(1, exhIds.length()));
			exhibition.setExhNames(exhNames.substring(1,exhNames.length()));
		}
		return exhibition;*/
		return super.get(id);
	}
	
	public List<Exhibition> findList(Exhibition exhibition) {
		return super.findList(exhibition);
	}
	
	public Page<Exhibition> findPage(Page<Exhibition> page, Exhibition exhibition) {
		exhibition.setPage(page);
		List<Exhibition> ebs = dao.findList(exhibition);
		List<Exhibition> resut = new ArrayList<Exhibition>();
		ExbExh exbExh = new ExbExh();
		for (Exhibition exb : ebs) {
			exbExh.setExhibition(exb);
			List<ExhHall> halls = exhHallDao.findAllExhHallByExhibition(exb.getId());
			exb.setExhHalls(halls);
			resut.add(exb);
		}
		page.setList(resut);
		return page;
	}
	
	@Transactional(readOnly = false)
	public void save(Exhibition exhibition) {
		String exhIds = exhibition.getExhIds();
		//修改
		if(!exhibition.getIsNewRecord()){
			ExbExh temp = new ExbExh();
			temp.setExhibition(exhibition);
			exbExhDao.delByExbition(temp);
			if(exhIds!=null && !"".equals(exhIds)&& !"null".equals(exhIds)){
				String[] ids = exhIds.split(",");
				for (String id : ids) {
					ExbExh exbExh = new ExbExh();				
					exbExh.setExhibition(exhibition);
					exbExh.setExhHall(exhHallDao.get(id));
					exbExh.preInsert();
					exbExhDao.insert(exbExh);
				}
			}
			exhibition.preUpdate();
			dao.update(exhibition);
		}else{//新增
			exhibition.preInsert();
			if(exhIds!=null && !"".equals(exhIds)&& !"null".equals(exhIds)){
				String[] ids = exhIds.split(",");
				for (String id : ids) {
					ExbExh exbExh = new ExbExh();				
					exbExh.setExhibition(exhibition);
					exbExh.setExhHall(exhHallDao.get(id));
					exbExh.preInsert();
					exbExhDao.insert(exbExh);
				}
			}
			dao.insert(exhibition);
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(Exhibition exhibition) {
		super.delete(exhibition);
	}
	
}